package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.GoodsTypeMoreAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.GoodsTypeMoreBean;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class GoodsTypeMoreDilaog extends Dialog {

    private final int x28;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.loading)
    CircularProgress loading;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    private Context context;
    private GoodsTypeMoreAdapter adapter;
    private boolean isHasProvinceData = false;
    private String id;
    private String name;

    public GoodsTypeMoreDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public GoodsTypeMoreDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        x28 = context.getResources().getDimensionPixelSize(R.dimen.x28);
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_goods_type);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 5);
        list.setLayoutManager(layoutManager);
        adapter = new GoodsTypeMoreAdapter(context);
        list.setAdapter(adapter);

        Spanned txt_tips = Spans.with(context).font("经营分类", x28, 0xff000000).style(Typeface.BOLD).
                font("（最多可选五个）", x28, 0xffCCCCCC).style(Typeface.BOLD).
                build();
        txtTips.setText(txt_tips);
    }

    public void getCateGory() {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);

        Api.api_service.getCateGory().compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    isHasProvinceData = true;
                    adapter.update(result.datas);
                });
    }

    @Override
    public void show() {
        super.show();
        if (!isHasProvinceData) {
            getCateGory();
        }
    }

    @OnClick({R.id.btn_close, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_submit:
                if (!adapter.isOneSelect()) {
                    ToastUtil.makeText(context, "请选择分类");
                    return;
                }
                if (onChooseListener != null) {

                    onChooseListener.onChooseType(adapter.getList());
                }
                dismiss();
                break;
        }
    }


    private OnChooseListener onChooseListener;

    public GoodsTypeMoreDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    public interface OnChooseListener {
        void onChooseType(List<GoodsTypeMoreBean> item);
    }
}
